package com.xz.group12306.servlet;

import java.io.IOException;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xz.group12306.business.common.Md5Utils;
import com.xz.group12306.business.common.TextUtils;
import com.xz.group12306.business.service.UserService;
import com.xz.group12306.business.service.impl.UserServiceImpl;
import com.xz.group12306.domain.User;

/**
 * @author 沈姚楠
 * 注册
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	    Statement state = null;
	     String sql = null;
	     ResultSet rs = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}
	
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
	    //获取表单数据
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String username=request.getParameter("username");
	    
		String password=request.getParameter("password");
		//String rule=request.getParameter("rule");
		String realname=request.getParameter("realname");
		String sex=request.getParameter("sex");
		//String city=request.getParameter("city");
		String certTypeId = request.getParameter("certType");
		String cert = request.getParameter("cert");
		String birthday = request.getParameter("birthday");
		String userType = request.getParameter("userType");
		String content = request.getParameter("content"); 
		UserService registerService=  new UserServiceImpl();
		
		//查询是否已存在该username
		User user=new User();
		user.setUsername(user.getUsername());
		User dbUser=registerService.getAllUser(user);
		//如果为null就说明该用户不存在，就允许注册
		if (dbUser == null) {	
			user.setUsername(username);
			user.setPassword(Md5Utils.md5(password));
			//user.setPassword(password);
			//user.setRule(rule);
			user.setRealname(realname);
			user.setSex(sex);

			// City
			
			//user.setCity(city);

			// CertType
			user.setCertType(certTypeId);

			// cert
			user.setCert(cert);

			// birthday
			if (!TextUtils.isEmpty(birthday)) {
			
				user.setBirthday(Date.valueOf(birthday));
			}
			  
			// UserType
			user.setUserType(userType);

			// content
			user.setContent(content);
			registerService.addUser(user);
			//注册成功跳到登录页面
			response.getWriter().print("<script> alert(\"注册成功!\");window.location.href='user/login.html'; </script>");
		} else {
			//否则显示用户重复，则继续返回注册页面
			response.getWriter().print("<script> alert(\"用户名重复!\");window.location.href='user/UserRegister.html'; </script>");
			
		}

	}
}
